package org.example.mallchat.service.impl;

import org.example.mallchat.cache.RoleCacheHandler;
import org.example.mallchat.dao.UserRoleDao;
import org.example.mallchat.domain.enums.RoleEnum;
import org.example.mallchat.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * <p>
 * 角色表 服务类
 * </p>
 *
 * @author 
 * @since 2024-12-25
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleCacheHandler roleCacheHandler;

    @Override
    public Long findPowerByUid(Long validUid) {
        // 获取该用户的所有角色
        Set<Long> roles = roleCacheHandler.getRolesByUid(validUid);
        // 判断该用户拥有的角色 1 超级管理员 2 普通用户
        if (hasAdmin(roles)){
            return RoleEnum.ADMIN.getId();
        }else {
            return RoleEnum.CHAT_MEMBER.getId();
        }
    }

    private boolean hasAdmin(Set<Long> roles){
        if (roles == null || roles.isEmpty()){
            return false;
        }else {
            return roles.contains(RoleEnum.ADMIN.getId());
        }
    }
}
